package org.jeecg.modules.basesource.handler;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basesource.constant.FpzlDmEnum;
import org.jeecg.modules.basesource.entity.FpCjDto;
import org.jeecg.modules.basesource.entity.FpSyncRecord;
import org.jeecg.modules.basesource.entity.ZxZzsfpJx;
import org.jeecg.modules.basesource.entity.ZxZzsfpXx;
import org.jeecg.modules.basesource.service.IFpSyncRecordService;
import org.jeecg.modules.basesource.service.IZxZzsfpJxService;
import org.jeecg.modules.basesource.service.IZxZzsfpXxService;
import org.jeecg.modules.dzswj.GuangdongSfjrApiService;
import org.jeecg.modules.dzswj.exception.SmdlException;
import org.jeecg.modules.dzswj.vo.LoginSessionVo;
import org.jeecg.modules.dzswj.vo.request.DzdzfpbccxReqVo;
import org.jeecg.modules.dzswj.vo.request.DzdzqdcxReqVo;
import org.jeecg.modules.dzswj.vo.response.fpxx.DzdzfpbccxVo;
import org.jeecg.modules.dzswj.vo.response.fpxx.DzdzqdcxVo;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.qyxx.entity.CjQyxxStatus;
import org.jeecg.modules.qyxx.entity.DjNsrxxPO;
import org.jeecg.modules.qyxx.service.ICjQyxxStatusService;
import org.jeecg.modules.qyxx.service.IDjNsrxxService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

@Slf4j
@Component
public class FpCjHandler {
    @Autowired
    private GuangdongSfjrApiService guangdongSfjrApiService;
    @Autowired
    private IZxZzsfpJxService zxZzsfpJxService;
    @Autowired
    private IDjOrgService djOrgService;
    @Autowired
    private IFpSyncRecordService fpSyncRecordService;
    @Autowired
    private IZxZzsfpXxService zxZzsfpXxService;
    @Autowired
    private ICjQyxxStatusService cjQyxxStatusService;
    @Autowired
    private GuangdongSfjrApiService sfjrApiService;
    @Autowired
    private IDjNsrxxService nsrxxService;

    // cjfs 全量- 0 增量- 1;
    private static final Integer INCREMENT = 1;
    private static final Integer FULL_DOSE = 0;

    // fplx_dm 进项发票- jxfp  销项发票- xxfp
    private static final String JXFP = "jxfp";
    private static final String XXFP = "xxfp";

    // fp_sync_status （001：未处理；002：处理中；003：已完成; 004:未签约; 005:用户信息为空; 006:密码错误; 010:未知错误）
    private static final String UNTREATED = "001";
    private static final String BEING_PROCESSED = "002";
    private static final String FINISHED = "003";
    private static final String UNAUTHORIZED = "011";

    private static ExecutorService executor = new ThreadPoolExecutor(10, 15, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10000),
            new ThreadPoolExecutor.CallerRunsPolicy());
    /*public ThreadPoolExecutor(int corePoolSize,//核心线程池大小
                              int maximumPoolSize,//最大线程池大小
                              long keepAliveTime,//线程池中超过corePoolSize数目的空闲线程最大存活时间；可以allowCoreThreadTimeOut(true)成为核心线程的有效时间
                              TimeUnit unit,//keepAliveTime的时间单位
                              BlockingQueue<Runnable> workQueue,//阻塞任务队列
                              ThreadFactory threadFactory,//线程工厂
                              RejectedExecutionHandler handler) {//当提交任务数超过maxmumPoolSize+workQueue之和时，任务会交给RejectedExecutionHandler来处理*/

    // 采集进项发票数据
    @XxlJob(value = "cjJxFpJob")
    public ReturnT<String> cjJxFpJob(String s) throws ParseException {
        List<Future> futureList = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        Date now = new Date();
        // 获取上个月的第一天
        String firstDayOfMonth = utils.getFirstDayOfMonth();
        // 获取上个月的最后一天
        String lastDayOfMonth = utils.getLastDayOfMonth();
        FpCjDto fpCjDto = new FpCjDto();
        fpCjDto.setFirstDayOfMonth(utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"));
        // fpCjDto.setLastDayOfMonth(utils.strToDate(lastDayOfMonth, "yyyy-MM-dd"));
        List<DjOrg> djOrgs = djOrgService.incrementListByShardTotal(fpCjDto);
        Date time = utils.strToDate(firstDayOfMonth, "yyyy-MM-dd");
        String kpyf = utils.getMonthToStr(time);
        LambdaQueryWrapper<FpSyncRecord> query = Wrappers.lambdaQuery(FpSyncRecord.class);
        query.eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getFplxDm, JXFP).last("limit 1");
        FpSyncRecord one = fpSyncRecordService.getOne(query);
        List<Date> dates = utils.findDates(firstDayOfMonth, lastDayOfMonth);
        // String lastTime = firstDayOfMonth;
        if (one == null) {
            // 初始化同步记录表数据
            fpSyncRecordService.init(JXFP, djOrgs, INCREMENT, dates);
        }
        for (DjOrg org : djOrgs) {
            LambdaQueryWrapper<CjQyxxStatus> lambdaQueryWrapper = Wrappers.lambdaQuery(CjQyxxStatus.class);
            lambdaQueryWrapper.eq(CjQyxxStatus::getStatus, 1).eq(CjQyxxStatus::getNsrsbh, org.getShxydm()).eq(CjQyxxStatus::getKssj, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd")).eq(CjQyxxStatus::getJssj, utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            CjQyxxStatus one1 = cjQyxxStatusService.getOne(lambdaQueryWrapper);
            if (one1 != null) {
                continue;
            }
            futureList.add(executor.submit(new Callable() { //多线程
                @Override
                public Object call() throws Exception {
                    boolean flag = true;
                    System.out.println("开始:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    for (Date date : dates) {
                        FpzlDmEnum[] fpzlDmEnums = FpzlDmEnum.values();
                        String kpyf = utils.getMonthToStr(date);
                        for (FpzlDmEnum fpzlDmEnum : fpzlDmEnums) {
                            try {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                LambdaQueryWrapper<FpSyncRecord> query1 = Wrappers.lambdaQuery(FpSyncRecord.class);
                                query1.eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).in(FpSyncRecord::getStatus, FINISHED,UNAUTHORIZED).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP);
                                FpSyncRecord one1 = fpSyncRecordService.getOne(query1);
                                if (one1 != null) {
                                    continue;
                                }

                                DzdzqdcxReqVo reqVo = new DzdzqdcxReqVo();
                                String day = simpleDateFormat.format(date);
                                reqVo.setCxsjq(day);
                                reqVo.setCxsjz(day.replace("00:00:00", "23:59:59"));
                                reqVo.setRwlx("LS");
                                // String kpyf = utils.getMonthToStr(date);
                                reqVo.setKpyf(kpyf);
                                reqVo.setFplx(JXFP);
                                reqVo.setNsrsbh(org.getShxydm());

                                LambdaUpdateWrapper<FpSyncRecord> wrapper = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                wrapper.set(FpSyncRecord::getStatus, BEING_PROCESSED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper);
                                reqVo.setFpzlDm(fpzlDmEnum.getValue());
                                // TODO 获取sessionId 如果redis没有的话就通过用户密码登录获取存到redis
                                LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(org.getShxydm());
                                DzdzqdcxVo dzdzqdcxVo = guangdongSfjrApiService.dzdzfpqdcx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                wrapper.clear();
                                if (dzdzqdcxVo == null) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                List<DzdzqdcxVo.QdData> qdDataList = dzdzqdcxVo.getQd();
                                if (qdDataList == null || qdDataList.size() <= 0) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                for (DzdzqdcxVo.QdData qdData : qdDataList) {
                                    ZxZzsfpJx jx = new ZxZzsfpJx();
                                    jx.setKpyf(qdData.getKpyf());
                                    if (!StringUtils.hasLength(qdData.getKprq())) {
                                        jx.setKprq(null);
                                    } else {
                                        jx.setKprq(simpleDateFormat.parse(qdData.getKprq()));
                                    }
                                    if (!StringUtils.hasLength(qdData.getZfrq())) {
                                        jx.setZfrq(null);
                                    } else {
                                        jx.setZfrq(simpleDateFormat.parse(qdData.getZfrq()));
                                    }
                                    jx.setFpdm(qdData.getFpdm());
                                    jx.setFpzldm(fpzlDmEnum.getValue());
                                    jx.setJe(qdData.getJe());
                                    jx.setFpztBz(qdData.getFpztBz());
                                    jx.setFphm(qdData.getFphm());
                                    jx.setJym(qdData.getJym());
                                    jx.setTynsrsbh(org.getShxydm());
                                    DzdzfpbccxReqVo vo = new DzdzfpbccxReqVo();
                                    vo.setFpzlDm(fpzlDmEnum.getValue());
                                    vo.setKpyf(qdData.getKpyf());
                                    DzdzfpbccxReqVo.Fpxx fpxx = new DzdzfpbccxReqVo.Fpxx();
                                    fpxx.setFpdm(qdData.getFpdm());
                                    fpxx.setFphm(qdData.getFphm());
                                    vo.setFpxx(Arrays.asList(fpxx));
                                    DzdzfpbccxVo dzdzfpbccxVo = guangdongSfjrApiService.dzdzfpbccx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), vo);
                                    List<DzdzfpbccxVo.MxVo> mx = dzdzfpbccxVo.getMx();
                                    List<DzdzfpbccxVo.ZbVo> zb = dzdzfpbccxVo.getZb();
                                    for (DzdzfpbccxVo.MxVo mxVo : mx) {
                                        jx.setGgxh(mxVo.getGgxh());
                                        jx.setXh(mxVo.getMxxh());
                                        jx.setJldw(mxVo.getJldw());
                                        jx.setDj(mxVo.getDj());
                                        jx.setYdfpbz(mxVo.getYdfpbz());
                                        jx.setZxbm(mxVo.getZxbm());
                                        jx.setSe(mxVo.getSe());
                                        jx.setSl(mxVo.getSl());
                                        jx.setSlv(mxVo.getSlv());
                                        jx.setSpbm(mxVo.getSpbm());
                                        if (mxVo.getQdbz() == null) {
                                            jx.setQdbzMx("");
                                        } else {
                                            jx.setQdbzMx(mxVo.getQdbz());
                                        }
                                        for (DzdzfpbccxVo.ZbVo zbVo : zb) {
                                            jx.setGfdzdh(zbVo.getGmfdzdh());
                                            jx.setGfdzdh(zbVo.getGmfyhzh());
                                            jx.setXfdzdh(zbVo.getXsfdzdh());
                                            jx.setXfyhzh(zbVo.getXsfyhzh());
                                            jx.setHjje(zbVo.getJe());
                                            jx.setHjse(zbVo.getSe());
                                            if (zbVo.getXhqdBz() == null) {
                                                jx.setQdbz("");
                                            } else {
                                                jx.setQdbz(zbVo.getXhqdBz());
                                            }
                                            jx.setXfsbh(zbVo.getXfsbh());
                                            jx.setGfmc(zbVo.getGfmc());
                                            jx.setYfpdm(zbVo.getYfpdm());
                                            jx.setYfphm(zbVo.getYfphm());
                                            jx.setJshj(zbVo.getJshj());
                                            jx.setXfmc(zbVo.getXfmc());
                                            jx.setZfrq(zbVo.getZfrq());
                                            jx.setBz(zbVo.getBz());
                                            jx.setMxts(zbVo.getMxts());
                                            jx.setGfsbh(zbVo.getGfsbh());
                                            jx.setJym(zbVo.getJym());
                                            jx.setGfDsswjgDm(zbVo.getGfDsswjgDm());
                                            jx.setXfDsswjgDm(zbVo.getXfDsswjgDm());
                                            jx.setXfQxswjgDm(zbVo.getXfQxswjgDm());
                                            jx.setXfSjswjgDm(zbVo.getXfSjswjgDm());
                                            jx.setGfQxswjgDm(zbVo.getGfQxswjgDm());
                                            jx.setGfSjswjgDm(zbVo.getGfSjswjgDm());
                                            jx.setFpztBz(zbVo.getFpztBz());
                                            jx.setFpztDm(zbVo.getFpztDm());
                                            jx.setDkXsfsbh(zbVo.getDkXsfsbh());
                                            jx.setDkXsfmc(zbVo.getDkXsfmc());
                                            jx.setCreateTime(now);
                                            jx.setUpdateTime(now);
                                            zxZzsfpJxService.saveOrUpdateByMultiId(jx);
                                        }
                                    }
                                }
                                wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper);
                            } catch (Exception e) {
                                log.error(e.getMessage());
                                String errorMsg = "";
                                String status = "";
                                LambdaUpdateWrapper<FpSyncRecord> wrapper1 = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                if (e instanceof SmdlException) {
                                    errorMsg = "【" + ((SmdlException) e).getErrorCode().getCode() + "】" + e.getMessage();
                                    status = "011";
                                } else {
                                    System.out.println("异常:" + org.getNsrmc());
                                    errorMsg = e.getMessage() + "异常:" + org.getNsrmc();
                                    status = "010";
                                }
                                flag = false;
                                wrapper1.set(FpSyncRecord::getStatus, status).set(FpSyncRecord::getRemark, errorMsg).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper1);
                            }
                        }
                    }
                    if (flag) {
                        cjQyxxStatusService.recordFpCompleteStatus(JXFP, org, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"), utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                    }
                    System.out.println("结束:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    return ReturnT.SUCCESS;
                }
            }));
        }
        /*} catch (ParseException e) {
            e.printStackTrace();
        }*/
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("错误信息:" + e);
                log.error("采集发票数据异常");
            }
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }

    @XxlJob("test")
    public ReturnT<String> test(String s) {
        List<Future> futureList = new ArrayList<>();
        // futureList.add(executor.submit(new Callable() { //多线程
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        for (String o : list) {
            futureList.add(executor.submit(new Callable() { //多线程
                @Override
                public Object call() throws Exception {
                    System.out.println(o);
                    Thread.sleep(10000);
                    return ReturnT.SUCCESS;
                }
            }));
        }
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("");
            }
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("testdemo")
    public ReturnT<String> testdemo(String s) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("===task start===");
                test("");
                System.out.println("===task finish===");
            }
        });
        thread.start();
        System.out.println("666");
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "cjXxFpJob")
    public ReturnT<String> cjXxFpJob(String s) {
        List<Future> futureList = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        Date now = new Date();
        // 获取上个月的第一天
        String firstDayOfMonth = utils.getFirstDayOfMonth();
        // 获取上个月的最后一天
        String lastDayOfMonth = utils.getLastDayOfMonth();
        FpCjDto fpCjDto = new FpCjDto();
        fpCjDto.setFirstDayOfMonth(utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"));
        // fpCjDto.setLastDayOfMonth(utils.strToDate(lastDayOfMonth, "yyyy-MM-dd"));
        List<DjOrg> djOrgs = djOrgService.incrementListByShardTotal(fpCjDto);
        Date time = DateUtils.getInstance().strToDate(firstDayOfMonth, "yyyy-MM-dd");
        String kpyf = DateUtils.getInstance().getMonthToStr(time);
        LambdaQueryWrapper<FpSyncRecord> query = Wrappers.lambdaQuery(FpSyncRecord.class);
        query.eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getFplxDm, XXFP).last("limit 1");
        FpSyncRecord one = fpSyncRecordService.getOne(query);
        List<Date> dates = utils.findDates(firstDayOfMonth, lastDayOfMonth);
        // String lastTime = firstDayOfMonth;
        if (one == null) {
            // 初始化同步记录表数据
            fpSyncRecordService.init(XXFP, djOrgs, INCREMENT, dates);
        }
        for (DjOrg org : djOrgs) {
            LambdaQueryWrapper<CjQyxxStatus> lambdaQueryWrapper = Wrappers.lambdaQuery(CjQyxxStatus.class);
            lambdaQueryWrapper.eq(CjQyxxStatus::getStatus, 1).eq(CjQyxxStatus::getNsrsbh, org.getShxydm()).eq(CjQyxxStatus::getKssj, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd")).eq(CjQyxxStatus::getJssj, utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            CjQyxxStatus one1 = cjQyxxStatusService.getOne(lambdaQueryWrapper);
            if (one1 != null) {
                continue;
            }
            futureList.add(executor.submit(new Callable() { //多线程
                @Override
                public Object call() throws Exception {
                    boolean flag = true;
                    System.out.println("开始:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    for (Date date : dates) {
                        String kpyf = utils.getMonthToStr(date);
                        FpzlDmEnum[] fpzlDmEnums = FpzlDmEnum.values();
                        for (FpzlDmEnum fpzlDmEnum : fpzlDmEnums) {
                            try {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                LambdaQueryWrapper<FpSyncRecord> query1 = Wrappers.lambdaQuery(FpSyncRecord.class);
                                query1.eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).in(FpSyncRecord::getStatus, FINISHED,UNAUTHORIZED).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP);
                                FpSyncRecord one1 = fpSyncRecordService.getOne(query1);
                                if (one1 != null) {
                                    continue;
                                }

                                DzdzqdcxReqVo reqVo = new DzdzqdcxReqVo();
                                String day = simpleDateFormat.format(date);
                                reqVo.setCxsjq(day);
                                reqVo.setCxsjz(day.replace("00:00:00", "23:59:59"));
                                reqVo.setRwlx("LS");
                                // String kpyf = utils.getMonthToStr(date);
                                reqVo.setKpyf(kpyf);
                                reqVo.setFplx(XXFP);
                                reqVo.setNsrsbh(org.getShxydm());

                                LambdaUpdateWrapper<FpSyncRecord> wrapper = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                wrapper.set(FpSyncRecord::getStatus, BEING_PROCESSED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper);
                                reqVo.setFpzlDm(fpzlDmEnum.getValue());
                                LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(org.getShxydm());
                                DzdzqdcxVo dzdzqdcxVo = guangdongSfjrApiService.dzdzfpqdcx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                wrapper.clear();
                                if (dzdzqdcxVo == null) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                List<DzdzqdcxVo.QdData> qdDataList = dzdzqdcxVo.getQd();
                                if (qdDataList == null || qdDataList.size() <= 0) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                for (DzdzqdcxVo.QdData qdData : qdDataList) {
                                    ZxZzsfpXx xx = new ZxZzsfpXx();
                                    xx.setKpyf(qdData.getKpyf());
                                    if (!StringUtils.hasLength(qdData.getKprq())) {
                                        xx.setKprq(null);
                                    } else {
                                        xx.setKprq(simpleDateFormat.parse(qdData.getKprq()));
                                    }
                                    if (!StringUtils.hasLength(qdData.getZfrq())) {
                                        xx.setZfrq(null);
                                    } else {
                                        xx.setZfrq(simpleDateFormat.parse(qdData.getZfrq()));
                                    }
                                    xx.setFpdm(qdData.getFpdm());
                                    xx.setFpzldm(fpzlDmEnum.getValue());
                                    xx.setJe(qdData.getJe());
                                    xx.setFpztBz(qdData.getFpztBz());
                                    xx.setFphm(qdData.getFphm());
                                    xx.setJym(qdData.getJym());
                                    xx.setTynsrsbh(org.getShxydm());
                                    DzdzfpbccxReqVo vo = new DzdzfpbccxReqVo();
                                    vo.setFpzlDm(fpzlDmEnum.getValue());
                                    vo.setKpyf(qdData.getKpyf());
                                    DzdzfpbccxReqVo.Fpxx fpxx = new DzdzfpbccxReqVo.Fpxx();
                                    fpxx.setFpdm(qdData.getFpdm());
                                    fpxx.setFphm(qdData.getFphm());
                                    vo.setFpxx(Arrays.asList(fpxx));
                                    DzdzfpbccxVo dzdzfpbccxVo = guangdongSfjrApiService.dzdzfpbccx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), vo);
                                    List<DzdzfpbccxVo.MxVo> mx = dzdzfpbccxVo.getMx();
                                    List<DzdzfpbccxVo.ZbVo> zb = dzdzfpbccxVo.getZb();
                                    for (DzdzfpbccxVo.MxVo mxVo : mx) {
                                        xx.setGgxh(mxVo.getGgxh());
                                        xx.setXh(mxVo.getMxxh());
                                        xx.setJldw(mxVo.getJldw());
                                        xx.setDj(mxVo.getDj());
                                        xx.setYdfpbz(mxVo.getYdfpbz());
                                        xx.setZxbm(mxVo.getZxbm());
                                        xx.setSe(mxVo.getSe());
                                        xx.setSl(mxVo.getSl());
                                        xx.setSlv(mxVo.getSlv());
                                        xx.setSpbm(mxVo.getSpbm());
                                        if (mxVo.getQdbz() == null) {
                                            xx.setQdbzMx("");
                                        } else {
                                            xx.setQdbzMx(mxVo.getQdbz());
                                        }
                                        for (DzdzfpbccxVo.ZbVo zbVo : zb) {
                                            xx.setGfdzdh(zbVo.getGmfdzdh());
                                            xx.setGfdzdh(zbVo.getGmfyhzh());
                                            xx.setXfdzdh(zbVo.getXsfdzdh());
                                            xx.setXfyhzh(zbVo.getXsfyhzh());
                                            xx.setHjje(zbVo.getJe());
                                            xx.setHjse(zbVo.getSe());
                                            if (zbVo.getXhqdBz() == null) {
                                                xx.setQdbz("");
                                            } else {
                                                xx.setQdbz(zbVo.getXhqdBz());
                                            }
                                            xx.setXfsbh(zbVo.getXfsbh());
                                            xx.setGfmc(zbVo.getGfmc());
                                            xx.setYfpdm(zbVo.getYfpdm());
                                            xx.setYfphm(zbVo.getYfphm());
                                            xx.setJshj(zbVo.getJshj());
                                            xx.setXfmc(zbVo.getXfmc());
                                            xx.setZfrq(zbVo.getZfrq());
                                            xx.setBz(zbVo.getBz());
                                            xx.setMxts(zbVo.getMxts());
                                            xx.setGfsbh(zbVo.getGfsbh());
                                            xx.setJym(zbVo.getJym());
                                            xx.setGfDsswjgDm(zbVo.getGfDsswjgDm());
                                            xx.setXfDsswjgDm(zbVo.getXfDsswjgDm());
                                            xx.setXfQxswjgDm(zbVo.getXfQxswjgDm());
                                            xx.setXfSjswjgDm(zbVo.getXfSjswjgDm());
                                            xx.setGfQxswjgDm(zbVo.getGfQxswjgDm());
                                            xx.setGfSjswjgDm(zbVo.getGfSjswjgDm());
                                            xx.setFpztBz(zbVo.getFpztBz());
                                            xx.setFpztDm(zbVo.getFpztDm());
                                            xx.setDkXsfsbh(zbVo.getDkXsfsbh());
                                            xx.setDkXsfmc(zbVo.getDkXsfmc());
                                            xx.setCreateTime(now);
                                            xx.setUpdateTime(now);
                                            zxZzsfpXxService.saveOrUpdateByMultiId(xx);
                                        }
                                    }
                                }
                                wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper);
                            } catch (Exception e) {
                                log.error(e.getMessage());
                                String errorMsg = "";
                                String status = "";
                                LambdaUpdateWrapper<FpSyncRecord> wrapper1 = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                if (e instanceof SmdlException) {
                                    errorMsg = "【" + ((SmdlException) e).getErrorCode().getCode() + "】" + e.getMessage();
                                    status = UNAUTHORIZED;
                                } else {
                                    System.out.println("异常:" + org.getNsrmc());
                                    errorMsg = e.getMessage() + "异常:" + org.getNsrmc();
                                    status = "010";
                                }
                                wrapper1.set(FpSyncRecord::getStatus, status).set(FpSyncRecord::getRemark, errorMsg).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, INCREMENT).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper1);
                                flag = false;
                            }
                        }
                    }
                    if (flag) {
                        cjQyxxStatusService.recordFpCompleteStatus(XXFP, org, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"), utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                    }
                    System.out.println("结束:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    return ReturnT.SUCCESS;
                }
            }));
        }
        /*} catch (ParseException e) {
            e.printStackTrace();
        }*/
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("错误信息:" + e);
                log.error("采集发票数据异常");
            }
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }

    // 采集销项发票数据(全量)
    @XxlJob(value = "cjQlXxFpJob")
    public ReturnT<String> cjQlXxFpJob(String s) {
        List<Future> futureList = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        Date now = new Date();
        // 获取四年前的一月一日
        String firstDayOfMonth = utils.getFourYearsAgoOneMonthFirstDay(now);
        // 获取上个月最后一天
        String lastDayOfMonth = utils.getLastDayOfMonth();
        FpCjDto fpCjDto = new FpCjDto();
        fpCjDto.setFirstDayOfMonth(utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"));
        // fpCjDto.setLastDayOfMonth(utils.strToDate(lastDayOfMonth, "yyyy-MM-dd"));
        List<DjOrg> djOrgs = djOrgService.fullDoseListByShardTotal(fpCjDto);
        Date time = DateUtils.getInstance().strToDate(firstDayOfMonth, "yyyy-MM-dd");
        String kpyf = DateUtils.getInstance().getMonthToStr(time);
        LambdaQueryWrapper<FpSyncRecord> query = Wrappers.lambdaQuery(FpSyncRecord.class);
        query.eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getFplxDm, XXFP).last("limit 1");
        FpSyncRecord one = fpSyncRecordService.getOne(query);
        List<Date> dates = utils.findDates(firstDayOfMonth, lastDayOfMonth);
        // String lastTime = firstDayOfMonth;
        if (one == null) {
            // 初始化同步记录表数据
            fpSyncRecordService.init(XXFP, djOrgs, FULL_DOSE, dates);
        }
        for (DjOrg org : djOrgs) {
            LambdaQueryWrapper<CjQyxxStatus> lambdaQueryWrapper = Wrappers.lambdaQuery(CjQyxxStatus.class);
            lambdaQueryWrapper.eq(CjQyxxStatus::getStatus, 1).eq(CjQyxxStatus::getNsrsbh, org.getShxydm()).eq(CjQyxxStatus::getKssj, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd")).eq(CjQyxxStatus::getJssj, utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            CjQyxxStatus one1 = cjQyxxStatusService.getOne(lambdaQueryWrapper);
            if (one1 != null) {
                continue;
            }
            futureList.add(executor.submit(new Callable() { //多线程
                @Override
                public Object call() throws Exception {
                    boolean flag = true;

                    System.out.println("开始:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    for (Date date : dates) {
                        String kpyf = utils.getMonthToStr(date);
                        FpzlDmEnum[] fpzlDmEnums = FpzlDmEnum.values();
                        for (FpzlDmEnum fpzlDmEnum : fpzlDmEnums) {
                            try {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                LambdaQueryWrapper<FpSyncRecord> query1 = Wrappers.lambdaQuery(FpSyncRecord.class);
                                query1.eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).in(FpSyncRecord::getStatus, FINISHED,UNAUTHORIZED).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP);
                                FpSyncRecord one1 = fpSyncRecordService.getOne(query1);
                                if (one1 != null) {
                                    continue;
                                }

                                DzdzqdcxReqVo reqVo = new DzdzqdcxReqVo();
                                String day = simpleDateFormat.format(date);
                                reqVo.setCxsjq(day);
                                reqVo.setCxsjz(day.replace("00:00:00", "23:59:59"));
                                reqVo.setRwlx("LS");
                                // String kpyf = utils.getMonthToStr(date);
                                reqVo.setKpyf(kpyf);
                                reqVo.setFplx(XXFP);
                                reqVo.setNsrsbh(org.getShxydm());

                                LambdaUpdateWrapper<FpSyncRecord> wrapper = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                wrapper.set(FpSyncRecord::getStatus, BEING_PROCESSED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper);
                                reqVo.setFpzlDm(fpzlDmEnum.getValue());
                                LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(org.getShxydm());
                                DzdzqdcxVo dzdzqdcxVo = guangdongSfjrApiService.dzdzfpqdcx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                wrapper.clear();
                                if (dzdzqdcxVo == null) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                List<DzdzqdcxVo.QdData> qdDataList = dzdzqdcxVo.getQd();
                                if (qdDataList == null || qdDataList.size() <= 0) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                for (DzdzqdcxVo.QdData qdData : qdDataList) {
                                    ZxZzsfpXx xx = new ZxZzsfpXx();
                                    xx.setKpyf(qdData.getKpyf());
                                    if (!StringUtils.hasLength(qdData.getKprq())) {
                                        xx.setKprq(null);
                                    } else {
                                        xx.setKprq(simpleDateFormat.parse(qdData.getKprq()));
                                    }
                                    if (!StringUtils.hasLength(qdData.getZfrq())) {
                                        xx.setZfrq(null);
                                    } else {
                                        xx.setZfrq(simpleDateFormat.parse(qdData.getZfrq()));
                                    }
                                    xx.setFpdm(qdData.getFpdm());
                                    xx.setFpzldm(fpzlDmEnum.getValue());
                                    xx.setJe(qdData.getJe());
                                    xx.setFpztBz(qdData.getFpztBz());
                                    xx.setFphm(qdData.getFphm());
                                    xx.setJym(qdData.getJym());
                                    xx.setTynsrsbh(org.getShxydm());
                                    DzdzfpbccxReqVo vo = new DzdzfpbccxReqVo();
                                    vo.setFpzlDm(fpzlDmEnum.getValue());
                                    vo.setKpyf(qdData.getKpyf());
                                    DzdzfpbccxReqVo.Fpxx fpxx = new DzdzfpbccxReqVo.Fpxx();
                                    fpxx.setFpdm(qdData.getFpdm());
                                    fpxx.setFphm(qdData.getFphm());
                                    vo.setFpxx(Arrays.asList(fpxx));
                                    DzdzfpbccxVo dzdzfpbccxVo = guangdongSfjrApiService.dzdzfpbccx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), vo);
                                    List<DzdzfpbccxVo.MxVo> mx = dzdzfpbccxVo.getMx();
                                    List<DzdzfpbccxVo.ZbVo> zb = dzdzfpbccxVo.getZb();
                                    for (DzdzfpbccxVo.MxVo mxVo : mx) {
                                        xx.setGgxh(mxVo.getGgxh());
                                        xx.setXh(mxVo.getMxxh());
                                        xx.setJldw(mxVo.getJldw());
                                        xx.setDj(mxVo.getDj());
                                        xx.setYdfpbz(mxVo.getYdfpbz());
                                        xx.setZxbm(mxVo.getZxbm());
                                        xx.setSe(mxVo.getSe());
                                        xx.setSl(mxVo.getSl());
                                        xx.setSlv(mxVo.getSlv());
                                        xx.setSpbm(mxVo.getSpbm());
                                        if (mxVo.getQdbz() == null) {
                                            xx.setQdbzMx("");
                                        } else {
                                            xx.setQdbzMx(mxVo.getQdbz());
                                        }
                                        for (DzdzfpbccxVo.ZbVo zbVo : zb) {
                                            xx.setGfdzdh(zbVo.getGmfdzdh());
                                            xx.setGfdzdh(zbVo.getGmfyhzh());
                                            xx.setXfdzdh(zbVo.getXsfdzdh());
                                            xx.setXfyhzh(zbVo.getXsfyhzh());
                                            xx.setHjje(zbVo.getJe());
                                            xx.setHjse(zbVo.getSe());
                                            if (zbVo.getXhqdBz() == null) {
                                                xx.setQdbz("");
                                            } else {
                                                xx.setQdbz(zbVo.getXhqdBz());
                                            }
                                            xx.setXfsbh(zbVo.getXfsbh());
                                            xx.setGfmc(zbVo.getGfmc());
                                            xx.setYfpdm(zbVo.getYfpdm());
                                            xx.setYfphm(zbVo.getYfphm());
                                            xx.setJshj(zbVo.getJshj());
                                            xx.setXfmc(zbVo.getXfmc());
                                            xx.setZfrq(zbVo.getZfrq());
                                            xx.setBz(zbVo.getBz());
                                            xx.setMxts(zbVo.getMxts());
                                            xx.setGfsbh(zbVo.getGfsbh());
                                            xx.setJym(zbVo.getJym());
                                            xx.setGfDsswjgDm(zbVo.getGfDsswjgDm());
                                            xx.setXfDsswjgDm(zbVo.getXfDsswjgDm());
                                            xx.setXfQxswjgDm(zbVo.getXfQxswjgDm());
                                            xx.setXfSjswjgDm(zbVo.getXfSjswjgDm());
                                            xx.setGfQxswjgDm(zbVo.getGfQxswjgDm());
                                            xx.setGfSjswjgDm(zbVo.getGfSjswjgDm());
                                            xx.setFpztBz(zbVo.getFpztBz());
                                            xx.setFpztDm(zbVo.getFpztDm());
                                            xx.setDkXsfsbh(zbVo.getDkXsfsbh());
                                            xx.setDkXsfmc(zbVo.getDkXsfmc());
                                            xx.setCreateTime(now);
                                            xx.setUpdateTime(now);
                                            zxZzsfpXxService.saveOrUpdateByMultiId(xx);
                                        }
                                    }
                                }
                                wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper);
                            } catch (Exception e) {
                                log.error(e.getMessage());
                                String errorMsg = "";
                                String status = "";
                                LambdaUpdateWrapper<FpSyncRecord> wrapper1 = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                if (e instanceof SmdlException) {
                                    errorMsg = "【" + ((SmdlException) e).getErrorCode().getCode() + "】" + e.getMessage();
                                    status = UNAUTHORIZED;
                                } else {
                                    System.out.println("异常:" + org.getNsrmc());
                                    errorMsg = e.getMessage() + "异常:" + org.getNsrmc();
                                    status = "010";
                                }
                                wrapper1.set(FpSyncRecord::getStatus, status).set(FpSyncRecord::getRemark, errorMsg).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, XXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper1);
                                flag = false;
                            }
                        }
                    }
                    if (flag) {
                        cjQyxxStatusService.recordFpCompleteStatus(XXFP, org, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"), utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                    }
                    System.out.println("结束:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    return ReturnT.SUCCESS;
                }
            }));
        }
        /*} catch (ParseException e) {
            e.printStackTrace();
        }*/
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("错误信息:" + e);
                log.error("采集发票数据异常");
            }
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "cjQlJxFpJob")
    public ReturnT<String> cjQlJxFpJob(String s) {
        List<Future> futureList = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        Date now = new Date();
        // 获取四年前的一月一日
        String firstDayOfMonth = utils.getFourYearsAgoOneMonthFirstDay(now);
        // 获取上个月最后一天
        String lastDayOfMonth = utils.getLastDayOfMonth();
        FpCjDto fpCjDto = new FpCjDto();
        fpCjDto.setFirstDayOfMonth(utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"));
        // fpCjDto.setLastDayOfMonth(utils.strToDate(lastDayOfMonth, "yyyy-MM-dd"));
        List<DjOrg> djOrgs = djOrgService.fullDoseListByShardTotal(fpCjDto);
        Date time = DateUtils.getInstance().strToDate(firstDayOfMonth, "yyyy-MM-dd");
        String kpyf = DateUtils.getInstance().getMonthToStr(time);
        LambdaQueryWrapper<FpSyncRecord> query = Wrappers.lambdaQuery(FpSyncRecord.class);
        query.eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getFplxDm, JXFP).last("limit 1");
        FpSyncRecord one = fpSyncRecordService.getOne(query);
        List<Date> dates = utils.findDates(firstDayOfMonth, lastDayOfMonth);
        // String lastTime = firstDayOfMonth;
        if (one == null) {
            // 初始化同步记录表数据
            fpSyncRecordService.init(JXFP, djOrgs, FULL_DOSE, dates);
        }
        for (DjOrg org : djOrgs) {
            LambdaQueryWrapper<CjQyxxStatus> lambdaQueryWrapper = Wrappers.lambdaQuery(CjQyxxStatus.class);
            lambdaQueryWrapper.eq(CjQyxxStatus::getStatus, 1).eq(CjQyxxStatus::getNsrsbh, org.getShxydm()).eq(CjQyxxStatus::getKssj, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd")).eq(CjQyxxStatus::getJssj, utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            CjQyxxStatus one1 = cjQyxxStatusService.getOne(lambdaQueryWrapper);
            if (one1 != null) {
                continue;
            }
            futureList.add(executor.submit(new Callable() { //多线程
                @Override
                public Object call() throws Exception {
                    System.out.println("开始:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    boolean flag = true;
                    for (Date date : dates) {
                        String kpyf = utils.getMonthToStr(date);
                        FpzlDmEnum[] fpzlDmEnums = FpzlDmEnum.values();
                        for (FpzlDmEnum fpzlDmEnum : fpzlDmEnums) {
                            try {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                LambdaQueryWrapper<FpSyncRecord> query1 = Wrappers.lambdaQuery(FpSyncRecord.class);
                                query1.eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, kpyf).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).in(FpSyncRecord::getStatus, FINISHED,UNAUTHORIZED).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP);
                                FpSyncRecord one1 = fpSyncRecordService.getOne(query1);
                                if (one1 != null) {
                                    continue;
                                }

                                DzdzqdcxReqVo reqVo = new DzdzqdcxReqVo();
                                String day = simpleDateFormat.format(date);
                                reqVo.setCxsjq(day);
                                reqVo.setCxsjz(day.replace("00:00:00", "23:59:59"));
                                reqVo.setRwlx("LS");
                                // String kpyf = utils.getMonthToStr(date);
                                reqVo.setKpyf(kpyf);
                                reqVo.setFplx(JXFP);
                                reqVo.setNsrsbh(org.getShxydm());

                                LambdaUpdateWrapper<FpSyncRecord> wrapper = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                wrapper.set(FpSyncRecord::getStatus, BEING_PROCESSED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                fpSyncRecordService.update(wrapper);
                                reqVo.setFpzlDm(fpzlDmEnum.getValue());
                                LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(org.getShxydm());
                                DzdzqdcxVo dzdzqdcxVo = guangdongSfjrApiService.dzdzfpqdcx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                wrapper.clear();
                                if (dzdzqdcxVo == null) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                List<DzdzqdcxVo.QdData> qdDataList = dzdzqdcxVo.getQd();
                                if (qdDataList == null || qdDataList.size() <= 0) {
                                    wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue()).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date));
                                    fpSyncRecordService.update(wrapper);
                                    continue;
                                }
                                for (DzdzqdcxVo.QdData qdData : qdDataList) {
                                    ZxZzsfpJx jx = new ZxZzsfpJx();
                                    jx.setKpyf(qdData.getKpyf());
                                    if (!StringUtils.hasLength(qdData.getKprq())) {
                                        jx.setKprq(null);
                                    } else {
                                        jx.setKprq(simpleDateFormat.parse(qdData.getKprq()));
                                    }
                                    if (!StringUtils.hasLength(qdData.getZfrq())) {
                                        jx.setZfrq(null);
                                    } else {
                                        jx.setZfrq(simpleDateFormat.parse(qdData.getZfrq()));
                                    }
                                    jx.setFpdm(qdData.getFpdm());
                                    jx.setFpzldm(fpzlDmEnum.getValue());
                                    jx.setJe(qdData.getJe());
                                    jx.setFpztBz(qdData.getFpztBz());
                                    jx.setFphm(qdData.getFphm());
                                    jx.setJym(qdData.getJym());
                                    jx.setTynsrsbh(org.getShxydm());
                                    DzdzfpbccxReqVo vo = new DzdzfpbccxReqVo();
                                    vo.setFpzlDm(fpzlDmEnum.getValue());
                                    vo.setKpyf(qdData.getKpyf());
                                    DzdzfpbccxReqVo.Fpxx fpxx = new DzdzfpbccxReqVo.Fpxx();
                                    fpxx.setFpdm(qdData.getFpdm());
                                    fpxx.setFphm(qdData.getFphm());
                                    vo.setFpxx(Arrays.asList(fpxx));
                                    DzdzfpbccxVo dzdzfpbccxVo = guangdongSfjrApiService.dzdzfpbccx(loginInfo.getSessionId(), loginInfo.getZgswjdm(), vo);
                                    List<DzdzfpbccxVo.MxVo> mx = dzdzfpbccxVo.getMx();
                                    List<DzdzfpbccxVo.ZbVo> zb = dzdzfpbccxVo.getZb();
                                    for (DzdzfpbccxVo.MxVo mxVo : mx) {
                                        jx.setGgxh(mxVo.getGgxh());
                                        jx.setXh(mxVo.getMxxh());
                                        jx.setJldw(mxVo.getJldw());
                                        jx.setDj(mxVo.getDj());
                                        jx.setYdfpbz(mxVo.getYdfpbz());
                                        jx.setZxbm(mxVo.getZxbm());
                                        jx.setSe(mxVo.getSe());
                                        jx.setSl(mxVo.getSl());
                                        jx.setSlv(mxVo.getSlv());
                                        jx.setSpbm(mxVo.getSpbm());
                                        if (mxVo.getQdbz() == null) {
                                            jx.setQdbzMx("");
                                        } else {
                                            jx.setQdbzMx(mxVo.getQdbz());
                                        }
                                        List<ZxZzsfpJx> list = new ArrayList<>();
                                        for (DzdzfpbccxVo.ZbVo zbVo : zb) {
                                            jx.setGfdzdh(zbVo.getGmfdzdh());
                                            jx.setGfdzdh(zbVo.getGmfyhzh());
                                            jx.setXfdzdh(zbVo.getXsfdzdh());
                                            jx.setXfyhzh(zbVo.getXsfyhzh());
                                            jx.setHjje(zbVo.getJe());
                                            jx.setHjse(zbVo.getSe());
                                            if (zbVo.getXhqdBz() == null) {
                                                jx.setQdbz("");
                                            } else {
                                                jx.setQdbz(zbVo.getXhqdBz());
                                            }
                                            jx.setXfsbh(zbVo.getXfsbh());
                                            jx.setGfmc(zbVo.getGfmc());
                                            jx.setYfpdm(zbVo.getYfpdm());
                                            jx.setYfphm(zbVo.getYfphm());
                                            jx.setJshj(zbVo.getJshj());
                                            jx.setXfmc(zbVo.getXfmc());
                                            jx.setZfrq(zbVo.getZfrq());
                                            jx.setBz(zbVo.getBz());
                                            jx.setMxts(zbVo.getMxts());
                                            jx.setGfsbh(zbVo.getGfsbh());
                                            jx.setJym(zbVo.getJym());
                                            jx.setGfDsswjgDm(zbVo.getGfDsswjgDm());
                                            jx.setXfDsswjgDm(zbVo.getXfDsswjgDm());
                                            jx.setXfQxswjgDm(zbVo.getXfQxswjgDm());
                                            jx.setXfSjswjgDm(zbVo.getXfSjswjgDm());
                                            jx.setGfQxswjgDm(zbVo.getGfQxswjgDm());
                                            jx.setGfSjswjgDm(zbVo.getGfSjswjgDm());
                                            jx.setFpztBz(zbVo.getFpztBz());
                                            jx.setFpztDm(zbVo.getFpztDm());
                                            jx.setDkXsfsbh(zbVo.getDkXsfsbh());
                                            jx.setDkXsfmc(zbVo.getDkXsfmc());
                                            jx.setCreateTime(now);
                                            jx.setUpdateTime(now);
                                            list.add(jx);
                                        }
                                        zxZzsfpJxService.saveOrUpdateBatchByMultiId(list, list.size());
                                    }
                                }
                                wrapper.set(FpSyncRecord::getStatus, FINISHED).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper);
                            } catch (Exception e) {
                                log.error(e.getMessage());
                                String errorMsg = "";
                                String status = "";
                                LambdaUpdateWrapper<FpSyncRecord> wrapper1 = Wrappers.lambdaUpdate(FpSyncRecord.class);
                                if (e instanceof SmdlException) {
                                    errorMsg = "【" + ((SmdlException) e).getErrorCode().getCode() + "】" + e.getMessage();
                                    status = UNAUTHORIZED;
                                } else {
                                    System.out.println("异常:" + org.getNsrmc());
                                    errorMsg = e.getMessage() + "异常:" + org.getNsrmc();
                                    status = "010";
                                }
                                wrapper1.set(FpSyncRecord::getStatus, status).set(FpSyncRecord::getRemark, errorMsg).set(FpSyncRecord::getUpdateTime, new Date()).eq(FpSyncRecord::getCjfs, FULL_DOSE).eq(FpSyncRecord::getFplxDm, JXFP).eq(FpSyncRecord::getCjrq, date).eq(FpSyncRecord::getNsrsbh, org.getShxydm()).eq(FpSyncRecord::getKpyf, utils.getMonthToStr(date)).eq(FpSyncRecord::getFpzlDm, fpzlDmEnum.getValue());
                                fpSyncRecordService.update(wrapper1);
                                flag = false;
                            }
                        }
                    }
                    if (flag) {
                        cjQyxxStatusService.recordFpCompleteStatus(JXFP, org, utils.strToDate(firstDayOfMonth, "yyyy-MM-dd"), utils.strToDate(lastDayOfMonth + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                    }
                    System.out.println("结束:" + System.currentTimeMillis() + ";企业:" + org.getNsrmc());
                    return ReturnT.SUCCESS;
                }
            }));
        }
        /*} catch (ParseException e) {
            e.printStackTrace();
        }*/
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("错误信息:" + e);
                log.error("采集发票数据异常");
            }
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }
}
